﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Data.Query;
using Nvigorate.Test.Extensions;

namespace Nvigorate.Test
{
    public static class QueryAssert
    {
        public static void InsertColumnValue(Insert insert, string property, object value)
        {
            var columnIndex = insert.ColumnList.FindIndex(c => ((InsertColumn) c).Name == property);
            Assert.AreNotEqual(-1, columnIndex);
            Assert.AreEqual(value.Quoted(), ((QueryValue) insert.InsertValues[columnIndex]).Value);
        }


        public static void NotEqualToNullCriterion(IClause clause, string fieldName)
        {
            var notEqual = clause as NotEqual;
            Assert.IsNotNull(notEqual);
            Assert.AreEqual(fieldName, ((Column) notEqual.Field).Name);
            Assert.IsInstanceOfType(notEqual.To, typeof (NullQueryObject));
        }

        public static void EqualCriterion(IClause clause, string fieldName, object value)
        {
            var equal = clause as Equal;
            Assert.IsNotNull(equal);
            Assert.AreEqual(fieldName, ((Column) equal.Field).Name);
            Assert.AreEqual(value, ((QueryValue) equal.To).Value);
        }

        public static void EqualCriterion(IClause clause, Literal literal, object value)
        {
            var equal = clause as Equal;
            Assert.IsNotNull(equal);
            Assert.AreEqual(literal.Value, ((Literal)equal.Field).Value);
            Assert.AreEqual(value, ((QueryValue)equal.To).Value);
        }

        public static void MaxFunction(IQueryObject clause, string columnName)
        {
            var ticketFunction = clause as Function;
            Assert.IsNotNull(ticketFunction);
            Assert.AreEqual(columnName, ((Column)ticketFunction.ParameterList[0]).Name);
            Assert.AreEqual("MAX", ticketFunction.Name);

        }

        public static void RaiseErrorEquals(IQueryObject error, int severity, long errorCode,
            string message)
        {
            var raiseError = error as RaiseError;
            Assert.IsNotNull(raiseError);
            Assert.AreEqual(severity, raiseError.Severity.Value);
            Assert.AreEqual(errorCode, raiseError.ErrorCode.Value);
            Assert.AreEqual(message, raiseError.ErrorMessage.Value);
        }

        public static void SchemaColumn(IQueryObject column, string columnName, Type columnType)
        {
            var schemaColumn = column as SchemaColumn;
            Assert.IsNotNull(schemaColumn);

            Assert.AreEqual(columnName, schemaColumn.Name);
            Assert.AreEqual(columnType, schemaColumn.ColumnType);

        }
    }
}